﻿using AbpApplication.InPut;
using CommunityToolkit.Mvvm.Input;
using MathNet.Numerics.LinearAlgebra.Factorization;
using Microsoft.EntityFrameworkCore.Query.Internal;
using NewWms.Apiservice;
using NewWms.Apiservice.Dto;
using NewWms.Extend;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
using Reconstruct_Desktop.Apiservice.Dto;
using Reconstruct_Desktop.Apiservice.InPut;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Reconstruct_Desktop.ViewModels.OrderManageViewModel
{
    public partial class PutOutOrderManageViewModel : BindableBase, INavigationAware
    {
        private readonly IViewService viewService;
        private readonly IBaseService baseService;
        private InPut_ViewService12 inPut_ViewService12;
        public InPut_ViewService12 InPut_ViewService12
        {
            get { return inPut_ViewService12; }
            set { inPut_ViewService12 = value; RaisePropertyChanged(); }
        }
        private List<PutOutOrderDto2> putOutOrderDto2s;
        public List<PutOutOrderDto2> PutOutOrderDto2s
        {
            get { return putOutOrderDto2s; }
            set { putOutOrderDto2s = value; RaisePropertyChanged(); }
        }
        private InPut_BaseService20 inPut_BaseService20;
        public InPut_BaseService20 InPut_BaseService20
        {
            get { return inPut_BaseService20; }
            set { inPut_BaseService20 = value;RaisePropertyChanged(); }
        }

        private InPut_BaseService21 inPut_BaseService21;
        public InPut_BaseService21 InPut_BaseService21
        {
            get { return inPut_BaseService21; }
            set { inPut_BaseService21 = value; RaisePropertyChanged(); }
        }


        private List<View_NeetOutMaterialDto> view_NeetOutMaterialDtos;

        public List<View_NeetOutMaterialDto> View_NeetOutMaterialDtos
        {
            get { return view_NeetOutMaterialDtos; }
            set { view_NeetOutMaterialDtos = value;RaisePropertyChanged(); }
        }

        private readonly IRegionManager _regionManager;

        private long count;
        public long Count
        {
            get { return count; }
            set { count = value; RaisePropertyChanged(); }
        }
        private long pagecount;
        public long PageCount
        {
            get { return pagecount; }
            set { pagecount = value; RaisePropertyChanged(); }
        }
        public PutOutOrderManageViewModel()
        {
            viewService = new ViewService();
            baseService = new BaseService();
            InPut_ViewService12 = new InPut_ViewService12();
            InPut_BaseService20 = new InPut_BaseService20();
            InPut_BaseService21=new InPut_BaseService21();
            PutOutOrderDto2s = new List<PutOutOrderDto2>();
            View_NeetOutMaterialDtos = new List<View_NeetOutMaterialDto>();
            InPut_ViewService12.PageIndex = 1;
            InPut_ViewService12.PageSize = 50;
            _regionManager=new RegionManager();


        }

        //获取出库工单
        [RelayCommand]
        private async void View_PutOutOrder()
        {
            Tuple<List<PutOutOrderDto2>, long?, long?> tuple = await viewService.View_PutOutOrder(InPut_ViewService12);
            if (tuple == null)
            {
                return;
            }
            PutOutOrderDto2s = tuple.Item1;
            Count = (long)tuple.Item2;
            PageCount = (long)tuple.Item3;
        }
        //分配出库工单
        [RelayCommand]
        private async  void AllocationOrder(string Symbol)
        {
          
            InPut_BaseService20.Symbol = Symbol;
            bool res=  await baseService.AllocationOrder(InPut_BaseService20);
            if (res)
            {
                View_Guid_NeetOutMaterial(Symbol);
            }
        }
        //解除分配
        [RelayCommand]
        private async void Deallocate(string Symbol)
        {
            bool res = await baseService.Deallocate(Symbol);
            if (res)
            {
                View_Guid_NeetOutMaterial(Symbol);
            }
        }
        //分配出库工单Agv任务
        [RelayCommand]
        private async void AllocationOrderAgvTask(string Symbol)
        {
            InPut_BaseService21.InlethouseCode=GlobalVariable.InletCode;
            InPut_BaseService21.Symbol = Symbol;
           bool res=   await baseService.AllocationOrderAgvTask(InPut_BaseService21);
            if (res)
            {
                View_Guid_NeetOutMaterial(Symbol);
            }
        }



        //获取分配后的清单
        [RelayCommand]
        private async void View_Guid_NeetOutMaterial(string Symbol)
        {
           Tuple<List<View_NeetOutMaterialDto>,bool> Tuple =  await viewService.View_Guid_NeetOutMaterial(Symbol);
            if (Tuple.Item2 == true)
            {
                View_NeetOutMaterialDtos=Tuple.Item1;
            }
            View_PutOutOrder();
        }


        //主页设置
        private string title = "出库清单";
        public string Title
        {
            get { return title; }
            set { title = value; RaisePropertyChanged(); }
        }
        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return true;
        }

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {
          
        }

        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            View_PutOutOrder();
        }
    }

}
